home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / z2bseq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  4.3 KB  |  152 lines

  1. /* Copyright (C) 1990 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* z2bseq.c */
  21. /* Level 2 binary object sequence operators */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "name.h"
  27. #include "i2btoken.h"
  28. #include "i2bseq.h"
  29.  
  30. typedef struct { ulong refs, chars; } bin_space;
  31.  
  32. /* Compute the size of a binary object sequence */
  33. private int
  34. bin_seq_space(ref *op, int array_ok, bin_space *sp)
  35. {    switch ( r_type(op) )
  36.       {
  37.       case t_null: case t_integer: case t_real:
  38.       case t_boolean: case t_mark:
  39.         sp->refs++;  break;
  40.       case t_string:
  41.         sp->refs++;  sp->chars += op->size;  break;
  42.       case t_name:
  43.         sp->refs++;
  44.         { ref nstr;
  45.           name_string_ref(op, &nstr);
  46.           sp->chars += nstr.size;
  47.         }
  48.         break;
  49.       case t_array:
  50.         if ( !array_ok ) return e_limitcheck;
  51.         sp->refs += op->size;
  52.         { uint i;
  53.           for ( i = 0; i < op->size; i++ )
  54.         { int code = bin_seq_space(op->value.refs + i, 0, sp);
  55.           if ( code < 0 ) return code;
  56.         }
  57.         }
  58.         break;
  59.       default:
  60.         return e_typecheck;
  61.       }
  62.     return 0;
  63. }
  64.  
  65. /* Write the objects part of a binary object sequence. */
  66. /* Return the new offset in the string part. */
  67. private uint
  68. bin_seq_write(stream *s, ref *op, byte tag, uint spos)
  69. {    bin_seq_obj ob;
  70.     ref nstr;
  71.     ob.unused = tag;
  72.     switch ( r_type(op) )
  73.       {
  74.       case t_null: ob.tx = bs_null; break;
  75.       case t_mark: ob.tx = bs_mark; break;
  76.       case t_integer: ob.tx = bs_integer; ob.value.w = op->value.intval;
  77. swb:                /* swap bytes of value if needed */
  78.         /***** NYI *****/
  79.         break;
  80.       case t_real: ob.tx = bs_real; ob.value.f = op->value.intval;
  81.         /***** handle non-IEEE native *****/
  82.         goto swb;
  83.       case t_boolean: ob.tx = bs_boolean; ob.value.w = op->value.index;
  84.         goto swb;
  85.       case t_array:
  86.         { uint i;
  87.           for ( i = 0; i < op->size; i++ )
  88.         spos = bin_seq_write(s, op->value.refs + i, 0, spos);
  89.         }
  90.         break;
  91.       case t_string: ob.tx = bs_string;
  92.         if ( r_has_attrs(op, a_executable) ) ob.tx += bs_executable;
  93. nos:        ob.size.w = op->size;
  94.                 /* *** swap bytes if needed *** */
  95.         ob.value.w = spos;
  96.         spos += op->size;
  97.         goto swb;
  98.       case t_name: ob.tx = bs_name;
  99.         name_string_ref(op, &nstr);
  100.         op = &nstr;
  101.         goto nos;
  102.       }
  103.     swrite(s, (byte *)&ob, sizeof(bin_seq_obj));
  104.     return spos;
  105. }
  106.  
  107. /* Write the string part of a binary object sequence */
  108. private void
  109. bin_seq_write_strings(stream *s, ref *op)
  110. {    switch ( r_type(op) )
  111.       {
  112.       case t_string:
  113.         swrite(s, op->value.bytes, op->size);
  114.         break;
  115.       case t_name:
  116.         { ref nstr;
  117.           name_string_ref(op, &nstr);
  118.           swrite(s, nstr.value.bytes, nstr.size);
  119.         }
  120.         break;
  121.       }
  122. }
  123.  
  124. /* Public interface for writing an object (printobject/writeobject) */
  125. int
  126. write_bin_object(register stream *s, byte tag, ref *op, int bin_format)
  127. {    bin_space space;
  128.     int code;
  129.     ulong total;
  130.     space.refs = space.chars = 0;
  131.     code = bin_seq_space(op, 1, &space);
  132.     if ( code < 0 ) return code;
  133.     /* Object has been validated, only possible error now is */
  134.     /* ioerror (which we don't check for). */
  135.     total = 4 + sizeof(bin_seq_obj) + space.refs * sizeof(bin_seq_obj) + space.chars;
  136.     if ( total > 0xffff ) return e_limitcheck;
  137.     sputc(s, bt_seq + bin_format);
  138.     sputc(s, 1);
  139.     sputshort(s, (ushort)total);
  140.     if ( r_type(op) == t_array )
  141.       { sputc(s, bs_array +
  142.           (r_has_attrs(op, a_executable) ? bs_executable : 0));
  143.         sputc(s, tag);
  144.         sputshort(s, op->size);
  145.         sputlong(s, 4 + sizeof(bin_seq_obj));
  146.         tag = 0;
  147.       }
  148.     bin_seq_write(s, op, tag, (uint)(total - space.chars));
  149.     bin_seq_write_strings(s, op);
  150.     return 0;
  151. }
  152.